package com.slkj.shilixiaoyuanapp.ui.teacher.tool.MyApproval;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.ToolOutWorkDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import butterknife.BindView;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.activity_sp_out_work, title = "出差")
public class SpOutWorkActivity extends BaseActivity {
    @InjectParam
    int aId;
    @InjectParam
    int id;
    @InjectParam
    ToolOutWorkDetailEntity data;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time) TextView tvEndTime;
    @BindView(R.id.edit_day)
    EditText editDay;
    @BindView(R.id.edit_where) EditText editWhere;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.tv_user_name) TextView tvUserName;
    @BindView(R.id.tv_now_day_time) TextView tvNowDayTime;
    @BindView(R.id.recycer_spr)
    RecyclerView recycerSpr;
    @BindView(R.id.layout_botton)
    View viewBotton;
    @BindView(R.id.layout_reson)
    View viewReson;
    @BindView(R.id.edit_reson)
    EditText editReson;
    SqrAdapter sqrAdapter;
    @Override
    protected void init() {
        SpOutWorkActivity__JumpCenter.bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(data.getInntime());
        editWhere.setText(data.getSite());
        tvStartTime.setText(data.getStartDate());
        tvEndTime.setText(data.getEndDate());
        editDay.setText(String.valueOf(data.getDuration()));
        editContent.setText(data.getBody());
        editReson.setText(data.getRefute());

        if (data.getAuditor() != null && !data.getAuditor().isEmpty()){
            recycerSpr.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));
            recycerSpr.setLayoutManager(new LinearLayoutManager(this));

            sqrAdapter = new SqrAdapter(data.getAuditor());
            recycerSpr.setAdapter(sqrAdapter);
        }
        switch (data.getState()){
            case MyApprovalHistoryFragment.TYPE_NOT_LOOK:
                viewBotton.setVisibility(View.VISIBLE);
                break;
            case MyApprovalHistoryFragment.TYPE_REFUSE:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
            case MyApprovalHistoryFragment.TYPE_OK:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
        }
        viewBotton.setVisibility(data.getMyState() == MyApprovalHistoryFragment.TYPE_NOT_LOOK?View.VISIBLE:View.GONE);
    }
    @OnClick(R.id.tv_tg)void tg(){
        String reson = editReson.getText().toString();
        upData(reson,2);

    }

    private void upData(String reson,int state) {
        HttpHeper.get().toolService()
                .spDo(id,aId,state,reson)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        //showToast(data);
                        LoadSuccessAndFailDialog.showSuccess(SpOutWorkActivity.this,data);
                        finish();
                    }
                });
    }

    @OnClick(R.id.tv_wtg)void wtg(){
        String reson = editReson.getText().toString();
        if (TextUtils.isEmpty(reson)){
            showToast("请填写拒绝原因");
            return;
        }
        upData(reson,1);
    }

}
